#include "fxcc/core/graph/directX11/RenderTargetJack.h"
#include "fxcc/core/graph/directX11/RenderStates.h"

using namespace fxcc::graph::directX11;

fxcc::graph::directX11::RenderTargetJack::RenderTargetJack(const fxcc::graph::common::RenderTargetJack::Desc& desc)
	:fxcc::graph::common::RenderTargetJack(desc)
{
	Load();
}

void fxcc::graph::directX11::RenderTargetJack::Load()
{
	m_Available = Init();
}

bool fxcc::graph::directX11::RenderTargetJack::Init()
{

	for (int i = 0; i < m_Desc.m_NumTextures; i++)
	{
		fxcc::graph::common::Texture2DJack::Desc texDesc;
		texDesc.m_Type = fxcc::graph::common::Texture2DJack::Desc::Type::_AllOC_;
		texDesc.m_MipmapData = m_Desc.m_MipmapData;
		texDesc.m_CreateRTV = true;
		texDesc.m_DepthStencilMap = false;
		m_Textures.push_back(std::make_shared<fxcc::graph::directX11::Texture2DJack>(texDesc));
	}

	fxcc::graph::common::Texture2DJack::Desc texDesc;
	texDesc.m_Type = fxcc::graph::common::Texture2DJack::Desc::Type::_AllOC_;
	texDesc.m_MipmapData = m_Desc.m_MipmapData;
	texDesc.m_MipmapData.m_NrComponent = 4;
	texDesc.m_CreateRTV = false;
	texDesc.m_DepthStencilMap = true;
	m_DepthStencilTexture = std::make_shared<fxcc::graph::directX11::Texture2DJack>(texDesc);

	return true;
}

void fxcc::graph::directX11::RenderTargetJack::RenderTargetJack::Begin()
{
	auto context = fxcc::graph::directX11::RenderStates::Inst().Context();
	std::vector<ID3D11RenderTargetView*> rtvs;
	for (int i = 0; i < m_Desc.m_NumTextures; i++)
	{
		rtvs.push_back(m_Textures[i]->m_RenderTargetView.Get());
	}

	context->OMSetRenderTargets(m_Desc.m_NumTextures, &rtvs[0], m_DepthStencilTexture->m_DepthStencilView.Get());
	D3D11_VIEWPORT viewport = {};
	viewport.TopLeftX = 0;
	viewport.TopLeftY = 0;
	viewport.Width = static_cast<float>(m_Desc.m_MipmapData.m_Width);
	viewport.Height = static_cast<float>(m_Desc.m_MipmapData.m_Height);
	viewport.MinDepth = 0.0f;
	viewport.MaxDepth = 1.0f;
	context->RSSetViewports(1, &viewport); context->RSSetViewports(1, &viewport);

}

void fxcc::graph::directX11::RenderTargetJack::RenderTargetJack::End()
{
	auto context = fxcc::graph::directX11::RenderStates::Inst().Context();
	context->OMSetRenderTargets(0, nullptr, nullptr);

}

